<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-05-29
 * Time: 10:00
 * 房间设施控制类
 */

namespace app\api\controller\v1;


use app\api\model\Goods as GoodsModel;
use app\api\model\GoodsImage;
use think\Db;
use think\Exception;
use think\facade\Request;

class Goods
{
    /**
     * 获取列表
     * @validate('PagingParameter')
     */
    public function getDataList($page,$limit,$name,$sort_id,$up_down)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        if($sort_id > 0) {
            $where[] = ['sort_id','eq',$sort_id];
        }
        $where[] = ['up_down','eq',$up_down];
        $query = GoodsModel::with('sort')->where($where)->order('order_sort desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['recommend'] == 0) {
                $item['recommend_name'] = '不推荐首页';
            } else {
                $item['recommend_name'] = '推荐首页';
            }
            if($item['up_down'] == 1) {
                $item['up_down_name'] = '上架';
            } elseif($item['up_down'] == 2) {
                $item['up_down_name'] = '下架';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     * @validate('IdMustBePositiveInt')
     */
    public function getDataInfo($id)
    {
        return GoodsModel::with('imgs.img')->where('id',$id)->find();
    }

    /**
     * 创建分类
     * @validate('CheckPrice')
     */
    public function createData()
    {
        $data = Request::post();
        //事务开始
        Db::startTrans();
        try{
            $mode = GoodsModel::create($data);
            $mode->imgs()->saveAll($data['imgs']);
            //事务结束  开始到结束中间出现错误以后,先执行的添加就不会生成记录
            Db::commit();
            return writeJson(201, [], '添加成功！');
        }
        catch (Exception $ex){
            //回滚事务
            Db::rollback();
            throw $ex;
        }
    }

    /**
     * 更新信息
     * @validate('CheckPrice')
     */
    public function updateData()
    {
        $data = Request::put();
        //事务开始
        Db::startTrans();
        try{
            GoodsImage::destroy(['goods_id' => $data['id']]);
            $mode = new GoodsModel();
            $mode->allowField(true)->save($data,['id',$data['id']]);
            // 图片
            foreach ($data['imgs'] as $k=>$v) {
                $data['imgs'][$k]['goods_id'] = $data['id'];
            }
            $imgs = new GoodsImage();
            $imgs->allowField(true)->saveAll($data['imgs']);
            //事务结束  开始到结束中间出现错误以后,先执行的添加就不会生成记录
            Db::commit();
            return writeJson(202, [], '修改成功！');
        }
        catch (Exception $ex){
            //回滚事务
            Db::rollback();
            throw $ex;
        }
    }

    /**
     * 删除信息
     * @validate('IdMustBePositiveInt')
     */
    public function deleteData($id)
    {
        GoodsModel::destroy($id);
        return writeJson(203, [], '删除成功！');
    }
}